import Joi from 'joi'
import { Result } from '~~/lib/helper/result'
import prisma from '~~/lib/prisma'

export default defineEventHandler(async (event) => {
  const userId = getLoginId(event)
  const params = await getQuery(event)
  const schema = Joi.object({
    notebookId: Joi.number().required(),
  })

  try {
    await schema.validateAsync(params)
  } catch (error: any) {
    return Result.Error('参数错误！' + error.message)
  }
  try {
    // const noteBooks = await prisma.notebookNote.findMany({
    //   where: { notebookId: +params.notebookId! },
    //   include: { note: true },
    // })
    const notes = await prisma.note.findMany({
      where: { notebookNotes: { some: { notebookId: +params.notebookId! } } },
    })
    return Result.Ok(notes, 200, '获取文章成功！')
  } catch (error) {
    console.log('error', error)
    return Result.Error('获取文章失败！')
  }
})
